package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * @description
 */
@Slf4j
@Service
public class GoodsServiceImpl implements GoodsService {
   
    @Autowired
    private GoodsDao goodsDao;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for (int i = 4; i > intCode.toString().length(); i--) {

            unitCode = "0" + unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    /**
     * 分页查询商品库存信息
     *
     * @param page        当前页
     * @param rows        每页显示条数
     * @param codeOrName  商品编码或名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    @Override
    public Map<String, Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        if (goodsTypeId != null) {
            goodsTypeId = goodsTypeId == 1 ? null : goodsTypeId;
        }
        HashMap<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        //从那条数据开始
        int offSet = (page - 1) * rows;
        //根据分页信息进行查找 商品的信息
        List<Goods> goodsList = goodsDao.listInventory(offSet, rows, codeOrName, goodsTypeId);
        goodsList.stream().forEach(goods -> {
            Integer saleTotal = 0;
            //查询销售总数
            Integer marketNum = goodsDao.marketNum(goods.getGoodsId());
            Integer returnNum = goodsDao.returnNum(goods.getGoodsId());
            if (marketNum != null) {
                if (returnNum != null) {
                    saleTotal = marketNum - returnNum;
                }
                saleTotal = marketNum;
            }
            goods.setSaleTotal(saleTotal);
            //类别
            String goodsTypeName = goodsDao.goodsTypeName(goods.getGoodsTypeId());
            goods.setGoodsTypeName(goodsTypeName);
        });
        map.put("total", goodsList.size());
        map.put("rows", goodsList);
        return map;
    }

    /**
     * 查询商品列表
     *
     * @param page
     * @param rows
     * @param goodsName
     * @param goodsTypeId
     * @return
     */
    @Override
    public Map<String, Object> goodList(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<Goods> sumArrList = new ArrayList<>();
        page = page == 0 ? 1 : page;
        //从那条数据开始
        int offSet = (page - 1) * rows;
        if (goodsTypeId == null) {
            List<Goods> goodsList = goodsDao.goodsList(offSet, rows, goodsName, goodsTypeId);
            sumArrList.addAll(goodsList);
        }
        //查询父节点的 list集合
        List<GoodsType> goodsTypeList1 = goodsDao.goodTypePid(goodsTypeId);
        //调用递归
        ArrayList<GoodsType> sumGoodsList = new ArrayList<>();
        List<GoodsType> goodsTypeList = this.DG(goodsTypeList1,sumGoodsList);
        //要是递归出来为 null 没有子节点
        if (!CollectionUtils.isEmpty(goodsTypeList)) {
            for (GoodsType goodsType : goodsTypeList) {
                List<Goods> goodsList = goodsDao.goodsList(offSet, rows, goodsName, goodsType.getGoodsTypeId());
                sumArrList.addAll(goodsList);
            }
        }//直接柑橘goodTypeID进行查询
            List<Goods> goodsList = goodsDao.goodsList(offSet, rows, goodsName, goodsTypeId);
            sumArrList.addAll(goodsList);
        map.put("total", sumArrList.size());
        for (Goods goods : sumArrList) {
            //类别
            String goodsTypeName = goodsDao.goodsTypeName(goods.getGoodsTypeId());
            goods.setGoodsTypeName(goodsTypeName);
        }
        map.put("rows", sumArrList);
        return map;
    }

    //传入一个父节点的list集合
    public List<GoodsType> DG(List<GoodsType> goodsTypeList,List<GoodsType> sumGoodsList) {
        if (!CollectionUtils.isEmpty(goodsTypeList)) {
            for (GoodsType goodsType : goodsTypeList) {
                sumGoodsList.add(goodsType);
            }
            for (GoodsType goodsType : goodsTypeList) {
                //子节点 的id  传进去        子子节点
                List<GoodsType> goodsTypeList1 = goodsDao.goodTypePid(goodsType.getGoodsTypeId());
                DG(goodsTypeList1,sumGoodsList);
            }
        }
        return sumGoodsList;
    }
    /**
     * 添加或修改商品信息
     *
     * @param goods 商品信息实体
     * @return
     */
    @Override
    public ServiceVO goodsSave(Goods goods) {
        if (goods.getGoodsId() == null) {
            //新增
            goods.setInventoryQuantity(0);
            goods.setGoodsId(0);
            goods.setLastPurchasingPrice(0);
            goods.setState(0);
            goodsDao.save(goods);
        } else {
            //修改
            goodsDao.update(goods);
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 删除商品信息
     *
     * @param goodsId 商品ID
     * @return
     */
    @Override
    public ServiceVO goodsDelete(String goodsId) {
        String state = goodsDao.getState(goodsId);
        if ("0".equals(state)) {
            goodsDao.delete(goodsId);
            return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        }

        return new ServiceVO<>(ErrorCode.STORED_ERROR_CODE, ErrorCode.STORED_ERROR_MESS);
    }

    /**
     * 分页查询无库存商品信息
     *
     * @param page       当前页
     * @param rows       每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        HashMap<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int getCount = (page - 1) * rows;
        List<Goods> goodsList = goodsDao.selectNoInventory(getCount, rows, nameOrCode);

        if (!CollectionUtils.isEmpty(goodsList)) {
            map.put("total", goodsList.size());
            map.put("rows", goodsList);
        }

        return map;
    }

    /**
     * 分页查询有库存商品信息
     *
     * @param page       当前页
     * @param rows       每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        HashMap<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int getCount = (page - 1) * rows;
        List<Goods> goodsList = goodsDao.getHasInventoryQuantity(getCount, rows, nameOrCode);
        if (!CollectionUtils.isEmpty(goodsList)) {
            map.put("total", goodsList.size());
            map.put("rows", goodsList);
        }
        return map;
    }

    /**
     * 添加商品期初库存
     *
     * @param goodsId           商品ID
     * @param inventoryQuantity 库存
     * @param purchasingPrice   成本价
     * @return
     */
    @Override
    public ServiceVO saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {

        goodsDao.saveUpdateStock(goodsId, inventoryQuantity, purchasingPrice);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 删除库存（要判断商品状态 入库、有进货和销售单据的不能删除）
     *
     * @param goodsId
     * @return
     */
    @Override
    public ServiceVO deleteStock(Integer goodsId) {
        Integer state = goodsDao.getStateByGoodsId(goodsId);
        if (state == 0) {
            goodsDao.delete(String.valueOf(goodsId));
            return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        } else {
            return new ServiceVO(ErrorCode.STORED_ERROR_CODE, ErrorCode.STORED_ERROR_MESS);
        }
    }

    /**
     * 查询库存报警商品信息
     *
     * @return
     */
    @Override
    public Map<String, Object> listAlarms() {
        HashMap<String, Object> map = new HashMap<>();
        //当前库存量 小于 库存下限的商品信息
        List<Goods> goodsList = goodsDao.listAlarms();
        if (!CollectionUtils.isEmpty(goodsList)) {
            map.put("rows", goodsList);
        }
        return map;
    }

    public static void main(String[] args) {
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(1);
        List<Integer> integers2 = new ArrayList<>();

        System.out.println(integers2);
    }
}
